package com.handinfo.android;

import android.app.Activity;
import android.app.AlertDialog;
//import android.app.DownloadManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.telephony.TelephonyManager;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Display;
import android.view.KeyEvent;
import android.view.Surface;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.handinfo.android.core.graphics.DWScreen;
import com.handinfo.android.core.input.DWInputView;
import com.handinfo.android.core.input.DWKey;
import com.handinfo.android.download.DownloadCompleteReceiver;
import com.handinfo.android.game.Login;
import com.handinfo.android.uicontrols.DWControlsManager;
//import com.handinfo.android.utils.ChannelOperation;
import com.handinfo.android.utils.DWChannel;
import com.handinfo.android.utils.Tools;
//import com.madhouse.android.trackingcode.SmartmadTrackingCode;
//import com.madhouse.android.trackingcode.SmartmadTrackingCodeListener;

public class DWGameActivity extends Activity
//implements SmartmadTrackingCodeListener
{
	/** Called when the activity is first created. */
	public static DWGameActivity instance;

	/** 当前view */
	protected DWGameView m_gameView;
	protected DWInputView m_inputView;
	protected DWFrameLayout m_layout;
	protected DWGLSurfaceView m_glCurrentView;

//	private DownloadCompleteReceiver m_download_complete_recerver;

	@Override
	public void onCreate(Bundle savedInstanceState)
	{
		Tools.debugPrintln("onCreate");
		super.onCreate(savedInstanceState);

		instance = this;
		//注释
//        SmartmadTrackingCode instance = SmartmadTrackingCode.getInstance();
//        instance.setTrackingListener(this);
//        instance.startTracking(this, "1640240567a06721");  //f6827c23bf5f2dd3



//		m_download_complete_recerver = new DownloadCompleteReceiver();
		getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

		//设置无标题全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
				WindowManager.LayoutParams.FLAG_FULLSCREEN);
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

		//设置亮度
		setScreenBrightness(0.25f);

		//设置横竖屏
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

		//设置屏幕宽高
		DisplayMetrics m_dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(m_dm);

		Display display = getWindowManager().getDefaultDisplay();

		//画布宽高
		int screenWidth = 800;
		int screenHeight = 480;
		//游戏宽高
		DWGameManager.Screen_Width = 800;
		DWGameManager.Screen_Height = 480;

		//如果屏幕已经旋转，则宽高互换
		if ((display.getRotation() == Surface.ROTATION_0 || display.getRotation() == Surface.ROTATION_180)
				&& (m_dm.widthPixels < m_dm.heightPixels)) {
			screenWidth = m_dm.heightPixels;
			screenHeight = m_dm.widthPixels;
		}
		else {
			screenWidth = m_dm.widthPixels;
			screenHeight = m_dm.heightPixels;
		}

		//大屏幕机型按照自身屏幕尺寸来设置成游戏尺寸T
		if (screenWidth > 1280 && screenHeight > 720) {
			DWGameManager.Screen_Width = 1280;
			DWGameManager.Screen_Height = 720;
			DWControlsManager.CONTROL_WIDTH = 1280;
			DWControlsManager.CONTROL_HEIGHT = 720;
			DWGameManager.AdpaterType = DWGameManager.AD_HIGH;
		}
		else if (screenWidth > 960 && screenWidth <= 1280)
		{
			DWGameManager.Screen_Width = screenWidth;
			DWGameManager.Screen_Height = screenHeight;
			DWControlsManager.CONTROL_WIDTH = DWGameManager.Screen_Width;
			DWControlsManager.CONTROL_HEIGHT = DWGameManager.Screen_Height;
			DWGameManager.AdpaterType = DWGameManager.AD_HIGH;
		}
		else if (screenWidth > 854 && screenWidth <= 960) {
			DWGameManager.Screen_Width = 960;
			DWGameManager.Screen_Height = 540;
			DWControlsManager.CONTROL_WIDTH = 800;
			DWControlsManager.CONTROL_HEIGHT = 480;
			DWGameManager.AdpaterType = DWGameManager.AD_MIDDLE;
		}
		else if (screenWidth < 800) {
			DWGameManager.Screen_Width = 800;
			DWGameManager.Screen_Height = 480;
			DWControlsManager.CONTROL_WIDTH = 800;
			DWControlsManager.CONTROL_HEIGHT = 480;
			DWGameManager.AdpaterType = DWGameManager.AD_LOW;
		}
		else {
			DWGameManager.Screen_Width = screenWidth;
			DWGameManager.Screen_Height = screenHeight;
			DWControlsManager.CONTROL_WIDTH = DWGameManager.Screen_Width;
			DWControlsManager.CONTROL_HEIGHT = DWGameManager.Screen_Height;
			DWGameManager.AdpaterType = DWGameManager.AD_MIDDLE;
		}
		//生成画布
		DWScreen screen = new DWScreen(screenWidth, screenHeight);

		DWGameManager.getInstance().setScreen(screen);

		// 初始化view
		initView();
		mHandler = new Handler();

		//游戏主线程启动
		DWGameManager.getInstance().setCurrentActivity(this);
		DWGameManager.getInstance().startThread();
		getSIMinfos();
		//注释
//		ChannelOperation.DWonCreate(this);
	}

	public Handler mHandler;

	@Override
	public void onConfigurationChanged(Configuration newConfig)
	{
		Tools.debugPrintln("onConfigurationChanged~~~~~~~~~~~~~");
		super.onConfigurationChanged(newConfig);
		//注释
//		ChannelOperation.onConfigurationChanged(this,newConfig);
	}

	protected void onStart()
	{
		Tools.debugPrintln("onStart");
//		if (getGameView() != null && !getGameView().isFocused()) {
//			getGameView().requestFocus();
//		}
		DWGameManager.getInstance().start();
		super.onStart();
		//注释
//		ChannelOperation.DWonStart(this);
	}

	@Override
	protected void onResume()
	{
		Tools.debugPrintln("onResume");
		DWGameManager.getInstance().resume();
//		registerReceiver(m_download_complete_recerver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
		super.onResume();
		//注释
//		ChannelOperation.DWonResume(this);
	}

	@Override
	protected void onPause()
	{
		Tools.debugPrintln("onPause");
		DWGameManager.getInstance().pause();
		super.onPause();
		//注释
//		ChannelOperation.DWonPause(this);
	}

	@Override
	protected void onStop()
	{
		Tools.debugPrintln("onStop");
		DWGameManager.getInstance().stop();
		super.onStop();
	}

	@Override
	protected void onDestroy()
	{
		Tools.debugPrintln("onDestroy");
//		if (m_download_complete_recerver != null) {
//			unregisterReceiver(m_download_complete_recerver);
//		}
		super.onDestroy();
		//注释
//		ChannelOperation.DWonDestroy(this);
	}

	/**
	 * 初始化View
	 */
	public void initView()
	{
		m_layout = new DWFrameLayout(this);
		m_gameView = new DWGameView(this);

		m_inputView = new DWInputView(this);
		m_layout.addView(m_inputView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT));
		m_layout.addView(m_gameView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
				ViewGroup.LayoutParams.FILL_PARENT));

//		m_glCurrentView = new DWGLSurfaceView(this);
//		m_layout.addView(m_glCurrentView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
//				ViewGroup.LayoutParams.FILL_PARENT));

		setContentView(m_layout);
	}

	public boolean onKeyDown(int keyCode, KeyEvent event)
	{
		if (DWKey.getInstance() != null) {
			DWKey.getInstance().onKeyDown(keyCode, event);
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean onKeyUp(int keyCode, KeyEvent event)
	{
		if (DWKey.getInstance() != null) {
			DWKey.getInstance().onKeyUp(keyCode, event);
		}
		return super.onKeyUp(keyCode, event);
	}

	@Override
	public void onBackPressed()
	{
		new AlertDialog.Builder(this).setTitle("提示").setIcon(null).setMessage(
				"您确定要退出游戏吗？").setPositiveButton("确定",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which)
					{

						DWGameManager.getInstance().destroy();
						// finish();
					}
				}).setNegativeButton("取消",
				new DialogInterface.OnClickListener() {

					public void onClick(DialogInterface dialog, int which)
					{
					}

				}).create().show();
		if (DWGameManager.getInstance().getGameState() != DWGameManager.GS_GAME) {
			Login m_login = new Login();
//			m_login.m_res_name = null;
//			m_login.m_res_password = null;
			m_login.m_register_name = null;
			m_login.m_register_password1 = null;
		}
	}

	/**
	 * 获得当前view
	 *
	 * @return
	 */
	public DWGameView getGameView()
	{
		return m_gameView;
	}

	public DWInputView getInputView()
	{
		return m_inputView;
	}

	public DWFrameLayout getFrameLayout()
	{
		return m_layout;
	}

	public Context getContext()
	{
		return getApplicationContext();
	}

	public void setScreenBrightness(float value)
	{
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		lp.screenBrightness = value;
		getWindow().setAttributes(lp);
	}

	public String m_imei;//IMEI
	public String m_number;//电话号码
	public String m_imsi;//IMSI
	public String m_model;//终端机型
	public byte m_phoneType;//信号类型 PHONE_TYPE_NONE 0  无信号  PHONE_TYPE_GSM  1 GSM信号 PHONE_TYPE_CDMA 2 CDMA信号

	public void getSIMinfos()
	{
		try {
			TelephonyManager telManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
			String WIDTH = "-" + DWScreen.Instance.m_width;
			String HEIGHT = "-" + DWScreen.Instance.m_height;
			if (telManager == null) {
				m_imei = "0" + WIDTH + HEIGHT;
				m_number = "0";
				m_imsi = "0";
				m_model = "0";
				return;
			}
			m_imei = telManager.getDeviceId(); //取出IMEI
			m_number = telManager.getLine1Number(); //取出MSISDN，很可能为空
			m_imsi = telManager.getSubscriberId(); //取出IMSI
			m_phoneType = (byte) telManager.getPhoneType();
			m_model = Build.MODEL;
			m_model = m_model.replace(" ", "_");
			if (m_imei == null || m_imei.equals("")) {
				m_imei = "0" + WIDTH + HEIGHT;
			}
			else {
				m_imei += WIDTH + HEIGHT;
			}
			if (m_number == null || m_number.equals("")) {
				m_number = "0";
			}
			if (m_imsi == null || m_imsi.equals("")) {
				m_imsi = "0";
			}
			if (m_model == null || m_model.equals("")) {
				m_model = "0";
			}
		}
		catch (Exception e) {
			Tools.debug(e);
		}
	}

	protected void onActivityResult(int requestCode, int resultCode, Intent data)
	{
		//注释
//		ChannelOperation.doonYYH(requestCode, resultCode, data);
		super.onActivityResult(requestCode, resultCode, data);
	}

	public void onTrackingStatus(int statusCode) {
		// TODO Auto-generated method stub
		Tools.debugPrintln(this.getClass().getName()+"statusCode:"+statusCode);
	}
}
